package com.weilaiwd.admin.aop;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;

public class ExceptionInterceptor implements Interceptor {

	@Override
	public void intercept(ActionInvocation ai) {
		Controller c = ai.getController();
		String requestType = c.getRequest().getHeader("X-Requested-With");
		if(StringUtils.isNotEmpty(requestType)) {
			//为ajax请求，抛异常时renderJson
			try {
				ai.invoke();
			} catch (Exception e) {
				e.printStackTrace();
				JSONObject json = new JSONObject();
				json.put("isSuccess", false);
				json.put("msg", "系统异常！！");
				c.renderJson(json.toString());
			}
		} else {
			ai.invoke();
		}
	}

}
